package com.yjhb.android.sbz.activity;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.entity.ClockInfo;
import com.yjhb.android.sbz.entity.ClockRecord;
import com.yjhb.android.sbz.manager.ResManager;
import com.yjhb.android.sbz.mvp.myrecord.MyRecordPresenter;
import com.yjhb.android.sbz.mvp.myrecord.MyRecordView;
import com.yjhb.android.sbz.view.SmallBallView;
import com.jojo.android.zxlib.tool.ZxTimeTool;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2021/1/12 15:33
 */
public class MyRecordActivity extends BaseActivity<MyRecordPresenter> implements MyRecordView {
    private final String TAG = "MyRecordActivity";
    @BindView(R.id.title_layout)
    RelativeLayout titleLayout;
    @BindView(R.id.tips_tv)
    TextView tipsTv;
    @BindView(R.id.betting_money_tv)
    TextView bettingMoneyTv;
    @BindView(R.id.income_tv)
    TextView incomeTv;
    @BindView(R.id.success_tv)
    TextView successTv;
    @BindView(R.id.record_list)
    LinearLayout recordList;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;

    @Override
    protected MyRecordPresenter initPresenter() {
        return new MyRecordPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_myrecord;
    }

    @Override
    protected void initUI() {

    }

    @Override
    protected void initData() {
        loadingDialog.show();
        presenter.getClockCollection();
        presenter.getClockList();
    }

    @Override
    protected void regEvent() {
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                presenter.getClockCollection();
                presenter.getClockList();
            }
        });
    }

    @OnClick(R.id.back)
    void back() {
        finish();
    }

    /**
     * 设置打卡记录数据
     *
     * @param data
     */
    private void setListData(List<ClockRecord> data) {
        if (data != null) {
            recordList.removeAllViews();
            for (int i = 0; i < data.size(); i++) {
                View view = LayoutInflater.from(this).inflate(R.layout.record_item, null);
                SmallBallView ballView = view.findViewById(R.id.ballView); //头部的圆点
                TextView date_tv = view.findViewById(R.id.date_tv); //投注时间
                TextView clock_time_tv = view.findViewById(R.id.clock_time_tv); //打卡时间
                TextView tips_tv = view.findViewById(R.id.tips_tv); //是否成功
                TextView income_tv = view.findViewById(R.id.income_tv);//打卡收益
                if (i == 0) {
                    ballView.setShowTopLine(false); //不显示上面的线
                }
                if (i == data.size() - 1) {
                    ballView.setShowBottomLine(false); //不显示下面的线
                }
                ClockRecord item = data.get(i);

                int state = item.getState();
                Date date = item.getDate();
                String amount = item.getBonus().toString();
                String cost = item.getMoney().toString();
                switch (state) {
                    case 0: //待打卡
                        ballView.setBallColor(0xfff0b100); //设置黄色颜色
                        tips_tv.setText("待打卡");
                        income_tv.setText("--");
                        clock_time_tv.setText("--");
                        break;
                    case 1://打卡成功
                        ballView.setBallColor(0xff33b422); //设置绿色颜色
                        tips_tv.setText("打卡成功");
                        income_tv.setTextColor(0xff33b422);
                        income_tv.setText("+" + amount + "元");
                        Date clockTime = item.getClockTime();
                        String time = ZxTimeTool.getDate(clockTime.getTime(), "MM.dd HH:mm:ss");
                        clock_time_tv.setText(time);
                        break;
                    case 2://打卡失败
                        ballView.setBallColor(0xffEB3339); //设置红色颜色
                        tips_tv.setText("打卡失败");
                        income_tv.setTextColor(0xffEB3339);
                        income_tv.setText("-" + cost + "元");
                        clock_time_tv.setText("--");
                        break;
                }
                if (date != null) {
                    String time = ZxTimeTool.getDate(date.getTime(), "MM.dd");
                    date_tv.setText(time);
                }

                recordList.addView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ResManager.dp() * 40));
            }
        }
    }

    @Override
    public void onFailure(int code, String msg) {
        loadingDialog.cancel();
        refreshLayout.setRefreshing(false);
    }


    @Override
    public void onLoad(List<ClockRecord> data) {
        setListData(data);
    }

    @Override
    public void onWait(String tips) {
        loadingDialog.setTipsText(tips);
        loadingDialog.show();
    }

    @Override
    public void onSuccess(ClockInfo clockInfo) {
        loadingDialog.cancel();
        refreshLayout.setRefreshing(false);
        if (clockInfo != null) {
            bettingMoneyTv.setText(clockInfo.getBetTotal().toString());
            incomeTv.setText(clockInfo.getBonusTotal().toString());
            successTv.setText("" + clockInfo.getSuccNum());
        }
    }
}
